﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wheeler.Domain;
using Wheeler.Common.Extensions;
using System.Reflection;


namespace Wheeler.Web.Utility
{
    public class BindSelectList
    {   
        //public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
        //    where T : class
        //{            
        //    SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
        //    SelectListItem item = new SelectListItem();            
        //    return selectList;            
        //}
        /// <summary>
        /// Binds a Collection to the Select List. Adds a new entry at first as (Please Select with blank value )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="itemList"></param>
        /// <param name="dataValueField"></param>
        /// <param name="dataTextField"></param>
        /// <returns></returns>
        public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
            where T : class
        {
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            PropertyInfo displayProperty = type.GetProperty(dataTextField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Text Field Property {0} was not found.", dataTextField));
            else
                displayProperty.SetValue(obj, "Please Select", null);

            displayProperty = type.GetProperty(dataValueField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Value Field Property {0} was not found.", dataValueField));
            else
                displayProperty.SetValue(obj, 0, null);

            if (itemList == null)
                itemList = new List<T>();

            itemList.Insert(0, (T)obj);
            SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
            return selectList;
        }

        
        public static SelectList PreferredPhone()
        {
            var phoneTypeList = new SelectList(new[]
                            {
                                new {ID="",Name="Please Select"},
                                new {ID="H",Name="Home"},
                                new {ID="M",Name="Mobile"},
                                new {ID="O",Name="Office"},
                            }, "ID", "Name");
            return phoneTypeList;
        }
        

        /// <summary>
        /// Generates Postdate Dropdownlist for Advanced Search Page
        /// </summary>
        /// <returns></returns>
        public static SelectList PostDates()
        {
            List<NameValue> dates = new List<NameValue>();
            dates.Add(new NameValue { ID = -1000, Name = "Any Date" });
            dates.Add(new NameValue { ID = +0, Name = "Today" });
            dates.Add(new NameValue { ID = -1, Name = "Yesterday" });
            dates.Add(new NameValue { ID = -7, Name = "Last 7 Days" });
            dates.Add(new NameValue { ID = -15, Name = "Last 15 Days" });
            dates.Add(new NameValue { ID = -30, Name = "Last 30 Days" });            
            return new SelectList(dates, "ID", "Name");
        }

        public static SelectList Groups()
        {
            WheelerEntities context = new WheelerEntities();
            List<Group> types = (from P in context.Groups orderby P.Name select P).ToList();
            return Bind<Group>(types, "ID", "Name");
        }
    }

    public class NameValue
    {
        public int ID { get; set; }
        public String Name { get; set; }
    }
}